package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserPostService;
import com.ctg.itrdc.sysmgr.portal.data.ISystemPostDao;
import com.ctg.itrdc.sysmgr.portal.data.ISystemUserPostDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.SystemPost;
import com.ctg.itrdc.sysmgr.portal.data.entity.SystemUserPost;
import com.ctg.itrdc.sysmgr.portal.data.vo.SystemPostVO;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;
import com.google.common.collect.Lists;

@Component("systemUserPostService")
public class SystemUserPostServiceImpl extends
		BaseService<SystemUserPost, Long, SystemUserPostDTO> implements
		ISystemUserPostService {
	@Resource
	private ISystemUserPostDao systemUserPostDao;
	@Resource
	private ISystemPostDao systemPostDao;

	@Override
	public Boolean existRelation(Long sysUserId, Long sysPostId) {
		List<SystemPostVO> voList = systemPostDao.selectByUId(sysUserId); // 走authentication缓存
		for (SystemPostVO systemPost : voList) {
			Long id = systemPost.getSysPostId();
			if (id == sysPostId) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void batchInsert(String ids, SystemUserPostDTO dto) {
		List<SystemUserPostDTO> dtoList = Lists.newArrayList();
		String[] arr = ids.split(",");
		for (String obj : arr) {
			long id = Long.parseLong(obj);
			SystemUserPostDTO tmp = BeanUtils.applyIfNotException(dto, SystemUserPostDTO.class);
			tmp.setSysPostId(id);
			if(!exists(tmp))
				dtoList.add(tmp);
		}
		if(CollectionUtils.isNotEmpty(dtoList)) {
//			this.batchInsert(dtoList);
			List<SystemUserPost> entities = BeanUtils.copyListNotException(dtoList, SystemUserPost.class);
			systemUserPostDao.jdbcBatchInsert(entities);
		}
	}
}
